/*
  Copyright 2009 Smart Enterprise Solution Corp.
  Email: contact@ses.vn - Website: http://www.ses.vn
  KimNgan Project.
*/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Data.SqlClient;
using SES.CMS.DO;
/// <summary>
/// Summary description for MediaDAL
/// </summary>
namespace SES.CMS.DAL
{

    public class MediaDAL : BaseDAL
    {
        #region Private Variables

        #endregion

        #region Public Constructors
        public MediaDAL()
        {
            //
            // TODO: Add constructor logic here
            //

        }
        #endregion



        #region Public Methods
        public int Insert(MediaDO objMediaDO)
        {

            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "spMedia_Insert";
            SqlParameter Sqlparam;

            Sqlparam = new SqlParameter("@MediaType", SqlDbType.Bit);
            Sqlparam.Value = objMediaDO.MediaType;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@MediaFile", SqlDbType.NVarChar);
            Sqlparam.Value = objMediaDO.MediaFile;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@Description", SqlDbType.NText);
            Sqlparam.Value = objMediaDO.Description;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@Title", SqlDbType.NVarChar);
            Sqlparam.Value = objMediaDO.Title;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@DateUpload", SqlDbType.DateTime);
            Sqlparam.Value = objMediaDO.DateUpload;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@MediaUrl", SqlDbType.NVarChar);
            Sqlparam.Value = objMediaDO.MediaUrl;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@OrderID", SqlDbType.Int);
            Sqlparam.Value = objMediaDO.OrderID;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@Published", SqlDbType.Bit);
            Sqlparam.Value = objMediaDO.Published;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@MetaTag", SqlDbType.NText);
            Sqlparam.Value = objMediaDO.MetaTag;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@ProductID", SqlDbType.Int);
            Sqlparam.Value = objMediaDO.ProductID;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@AlbumID", SqlDbType.Int);
            Sqlparam.Value = objMediaDO.AlbumID;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@IsHomePage", SqlDbType.Bit);
            Sqlparam.Value = objMediaDO.IsHomePage;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@Thumbnail", SqlDbType.NVarChar);
            Sqlparam.Value = objMediaDO.Thumbnail;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@ID", SqlDbType.Int);
            Sqlparam.Direction = ParameterDirection.ReturnValue;
            Sqlcomm.Parameters.Add(Sqlparam);


            int result = base.ExecuteNoneQuery(Sqlcomm);

            if (!Convert.IsDBNull(Sqlcomm.Parameters["@ID"]))
                result = Convert.ToInt32(Sqlcomm.Parameters["@ID"].Value);

            return result;
        }

        public int Update(MediaDO objMediaDO)
        {

            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "spMedia_UpdateByPK";
            SqlParameter Sqlparam;

            Sqlparam = new SqlParameter("@MediaID", SqlDbType.Int);
            Sqlparam.Value = objMediaDO.MediaID;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@MediaType", SqlDbType.Bit);
            Sqlparam.Value = objMediaDO.MediaType;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@MediaFile", SqlDbType.NVarChar);
            Sqlparam.Value = objMediaDO.MediaFile;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@Description", SqlDbType.NText);
            Sqlparam.Value = objMediaDO.Description;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@Title", SqlDbType.NVarChar);
            Sqlparam.Value = objMediaDO.Title;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@DateUpload", SqlDbType.DateTime);
            Sqlparam.Value = objMediaDO.DateUpload;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@MediaUrl", SqlDbType.NVarChar);
            Sqlparam.Value = objMediaDO.MediaUrl;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@OrderID", SqlDbType.Int);
            Sqlparam.Value = objMediaDO.OrderID;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@Published", SqlDbType.Bit);
            Sqlparam.Value = objMediaDO.Published;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@MetaTag", SqlDbType.NText);
            Sqlparam.Value = objMediaDO.MetaTag;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@ProductID", SqlDbType.Int);
            Sqlparam.Value = objMediaDO.ProductID;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@AlbumID", SqlDbType.Int);
            Sqlparam.Value = objMediaDO.AlbumID;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@IsHomePage", SqlDbType.Bit);
            Sqlparam.Value = objMediaDO.IsHomePage;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@Thumbnail", SqlDbType.NVarChar);
            Sqlparam.Value = objMediaDO.Thumbnail;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@ErrorCode", SqlDbType.Int);
            Sqlparam.Direction = ParameterDirection.ReturnValue;
            Sqlcomm.Parameters.Add(Sqlparam);

            int result = base.ExecuteNoneQuery(Sqlcomm);

            if (!Convert.IsDBNull(Sqlcomm.Parameters["@ErrorCode"]))
                result = Convert.ToInt32(Sqlcomm.Parameters["@ErrorCode"].Value);

            return result;


        }

        public int Delete(MediaDO objMediaDO)
        {

            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "spMedia_DeleteByPK";
            SqlParameter Sqlparam;


            Sqlparam = new SqlParameter("@MediaID", SqlDbType.Int);
            Sqlparam.Value = objMediaDO.MediaID;
            Sqlcomm.Parameters.Add(Sqlparam);



            int result = base.ExecuteNoneQuery(Sqlcomm);
            return result;
        }

        public int DeleteAll()
        {

            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "spMedia_DeleteAll";

            int result = base.ExecuteNoneQuery(Sqlcomm);
            return result;
        }

        public MediaDO Select(MediaDO objMediaDO)
        {

            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "spMedia_GetByPK";
            SqlParameter Sqlparam;


            Sqlparam = new SqlParameter("@MediaID", SqlDbType.Int);
            Sqlparam.Value = objMediaDO.MediaID;
            Sqlcomm.Parameters.Add(Sqlparam);



            DataSet ds = base.GetDataSet(Sqlcomm);
            DataRow dr = null;
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                dr = ds.Tables[0].Rows[0];
                if (!Convert.IsDBNull(dr["MediaID"]))
                    objMediaDO.MediaID = Convert.ToInt32(dr["MediaID"]);
                if (!Convert.IsDBNull(dr["MediaType"]))
                    objMediaDO.MediaType = Convert.ToBoolean(dr["MediaType"]);
                if (!Convert.IsDBNull(dr["MediaFile"]))
                    objMediaDO.MediaFile = Convert.ToString(dr["MediaFile"]);
                if (!Convert.IsDBNull(dr["Description"]))
                    objMediaDO.Description = Convert.ToString(dr["Description"]);
                if (!Convert.IsDBNull(dr["Title"]))
                    objMediaDO.Title = Convert.ToString(dr["Title"]);
                if (!Convert.IsDBNull(dr["DateUpload"]))
                    objMediaDO.DateUpload = Convert.ToDateTime(dr["DateUpload"]);
                if (!Convert.IsDBNull(dr["MediaUrl"]))
                    objMediaDO.MediaUrl = Convert.ToString(dr["MediaUrl"]);
                if (!Convert.IsDBNull(dr["OrderID"]))
                    objMediaDO.OrderID = Convert.ToInt32(dr["OrderID"]);
                if (!Convert.IsDBNull(dr["Published"]))
                    objMediaDO.Published = Convert.ToBoolean(dr["Published"]);
                if (!Convert.IsDBNull(dr["MetaTag"]))
                    objMediaDO.MetaTag = Convert.ToString(dr["MetaTag"]);
                if (!Convert.IsDBNull(dr["ProductID"]))
                    objMediaDO.ProductID = Convert.ToInt32(dr["ProductID"]);
                if (!Convert.IsDBNull(dr["AlbumID"]))
                    objMediaDO.AlbumID = Convert.ToInt32(dr["AlbumID"]);
                if (!Convert.IsDBNull(dr["IsHomePage"]))
                    objMediaDO.IsHomePage = Convert.ToBoolean(dr["IsHomePage"]);
                if (!Convert.IsDBNull(dr["Thumbnail"]))
                    objMediaDO.Thumbnail = Convert.ToString(dr["Thumbnail"]);

            }
            return objMediaDO;
        }

        public ArrayList SelectAll1()
        {

            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "spMedia_GetAll";

            DataSet ds = base.GetDataSet(Sqlcomm);
            DataTable dt = null;
            ArrayList arrMediaDO = new ArrayList();
            if (ds != null && ds.Tables.Count > 0)
            {
                dt = ds.Tables[0];
                foreach (DataRow dr in dt.Rows)
                {
                    MediaDO objMediaDO = new MediaDO();
                    if (!Convert.IsDBNull(dr["MediaID"]))
                        objMediaDO.MediaID = Convert.ToInt32(dr["MediaID"]);
                    if (!Convert.IsDBNull(dr["MediaType"]))
                        objMediaDO.MediaType = Convert.ToBoolean(dr["MediaType"]);
                    if (!Convert.IsDBNull(dr["MediaFile"]))
                        objMediaDO.MediaFile = Convert.ToString(dr["MediaFile"]);
                    if (!Convert.IsDBNull(dr["Description"]))
                        objMediaDO.Description = Convert.ToString(dr["Description"]);
                    if (!Convert.IsDBNull(dr["Title"]))
                        objMediaDO.Title = Convert.ToString(dr["Title"]);
                    if (!Convert.IsDBNull(dr["DateUpload"]))
                        objMediaDO.DateUpload = Convert.ToDateTime(dr["DateUpload"]);
                    if (!Convert.IsDBNull(dr["MediaUrl"]))
                        objMediaDO.MediaUrl = Convert.ToString(dr["MediaUrl"]);
                    if (!Convert.IsDBNull(dr["OrderID"]))
                        objMediaDO.OrderID = Convert.ToInt32(dr["OrderID"]);
                    if (!Convert.IsDBNull(dr["Published"]))
                        objMediaDO.Published = Convert.ToBoolean(dr["Published"]);
                    if (!Convert.IsDBNull(dr["MetaTag"]))
                        objMediaDO.MetaTag = Convert.ToString(dr["MetaTag"]);
                    if (!Convert.IsDBNull(dr["ProductID"]))
                        objMediaDO.ProductID = Convert.ToInt32(dr["ProductID"]);
                    if (!Convert.IsDBNull(dr["AlbumID"]))
                        objMediaDO.AlbumID = Convert.ToInt32(dr["AlbumID"]);
                    if (!Convert.IsDBNull(dr["IsHomePage"]))
                        objMediaDO.IsHomePage = Convert.ToBoolean(dr["IsHomePage"]);
                    if (!Convert.IsDBNull(dr["Thumbnail"]))
                        objMediaDO.Thumbnail = Convert.ToString(dr["Thumbnail"]);
                    arrMediaDO.Add(objMediaDO);
                }
            }
            return arrMediaDO;
        }

        public DataTable SelectAll()
        {

            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "spMedia_GetAll";

            DataSet ds = base.GetDataSet(Sqlcomm);
            DataTable dt = null;

            if (ds != null && ds.Tables.Count > 0)
            {
                dt = ds.Tables[0];

            }
            return dt;
        }

        public int UpdateHomePageVideo(int mediaID)
        {

            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "spMedia_VideoHomePage";
            SqlParameter Sqlparam;


            Sqlparam = new SqlParameter("@MediaID", SqlDbType.Int);
            Sqlparam.Value = mediaID;
            Sqlcomm.Parameters.Add(Sqlparam);


            Sqlparam = new SqlParameter("@ID", SqlDbType.Int);
            Sqlparam.Direction = ParameterDirection.ReturnValue;
            Sqlcomm.Parameters.Add(Sqlparam);


            int result = base.ExecuteNoneQuery(Sqlcomm);

            if (!Convert.IsDBNull(Sqlcomm.Parameters["@ID"]))
                result = Convert.ToInt32(Sqlcomm.Parameters["@ID"].Value);

            return result;
        }
        public DataTable GetHomePageVideo()
        {
            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "spMedia_GetVideoHomePage";

            DataSet ds = base.GetDataSet(Sqlcomm);
            DataTable dt = null;

            if (ds != null && ds.Tables.Count > 0)
            {
                dt = ds.Tables[0];

            }
            return dt;
        }
        public DataTable GetByAlbum(int albumID)
        {

            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "spMedia_GetByAlbumID";
            SqlParameter Sqlparam;


            Sqlparam = new SqlParameter("@AlbumID", SqlDbType.Int);
            Sqlparam.Value = albumID;
            Sqlcomm.Parameters.Add(Sqlparam);


            DataSet ds = base.GetDataSet(Sqlcomm);
            DataTable dt = null;

            if (ds != null && ds.Tables.Count > 0)
            {
                dt = ds.Tables[0];

            }
            return dt;
        }

        public DataTable GetTopProductID(int productID, int top)
        {
            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "spMedia_GetTopProductId";
            SqlParameter Sqlparam;


            Sqlparam = new SqlParameter("@ProductId", SqlDbType.Int);
            Sqlparam.Value = productID;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@Top", SqlDbType.Int);
            Sqlparam.Value = top;
            Sqlcomm.Parameters.Add(Sqlparam);

            DataSet ds = base.GetDataSet(Sqlcomm);
            DataTable dt = null;

            if (ds != null && ds.Tables.Count > 0)
            {
                dt = ds.Tables[0];

            }
            return dt;
        }
        public DataTable GetByType(bool MediaType)
        {

            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "spMedia_GetByType";
            SqlParameter Sqlparam;


            Sqlparam = new SqlParameter("@MediaType", SqlDbType.Bit);
            Sqlparam.Value = MediaType;
            Sqlcomm.Parameters.Add(Sqlparam);


            DataSet ds = base.GetDataSet(Sqlcomm);
            DataTable dt = null;

            if (ds != null && ds.Tables.Count > 0)
            {
                dt = ds.Tables[0];

            }
            return dt;
        }
        #endregion
        public DataTable SelectBySanPhamIDAdmin(int ProductID, bool MediaType)
        {
            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "Media_SelectBySanPhamID";
            SqlParameter Sqlparam;


            Sqlparam = new SqlParameter("@ProductID", SqlDbType.Int);
            Sqlparam.Value = ProductID;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@MediaType", SqlDbType.Bit);
            Sqlparam.Value = MediaType;
            Sqlcomm.Parameters.Add(Sqlparam);

            DataSet ds = base.GetDataSet(Sqlcomm);
            DataTable dt = null;

            if (ds != null && ds.Tables.Count > 0)
            {
                dt = ds.Tables[0];

            }
            return dt;
        }

    }

}
